package org.bing.logging;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.eclipse.jetty.util.log.Logger;

public class JettySlf4jLogger implements Logger {
    private static final Map<String, JettySlf4jAdapter> loggerMap = new HashMap<String, JettySlf4jAdapter>();
    private final org.bing.adapter.org.slf4j.v161.Logger _logger;

    public JettySlf4jLogger() {
        this("org.eclipse.jetty.util.log");
    }

    public JettySlf4jLogger(String name) {
        _logger = getLogger2(name);
    }

    private JettySlf4jAdapter getLogger2(String name) {
        JettySlf4jAdapter logger = null;
        // protect against concurrent access of loggerMap
        synchronized (this) {
            logger = loggerMap.get(name);
            if (logger == null) {
                org.apache.log4j.Logger log4jLogger;
                if (name.equalsIgnoreCase(org.bing.adapter.org.slf4j.v161.Logger.ROOT_LOGGER_NAME)) {
                    log4jLogger = LogManager.getRootLogger();
                } else {
                    log4jLogger = LogManager.getLogger(name);
                }
                logger = new JettySlf4jAdapter(log4jLogger);
                loggerMap.put(name, logger);
            }
        }
        return logger;
    }

    public String getName() {
        return _logger.getName();
    }

    public void warn(String msg, Object... args) {
        _logger.warn(msg, args);
    }

    public void warn(Throwable thrown) {
        warn("", thrown);
    }

    public void warn(String msg, Throwable thrown) {
        _logger.warn(msg, thrown);
    }

    public void info(String msg, Object... args) {
        _logger.info(msg, args);
    }

    public void info(Throwable thrown) {
        info("", thrown);
    }

    public void info(String msg, Throwable thrown) {
        _logger.info(msg, thrown);
    }

    public void debug(String msg, Object... args) {
        _logger.debug(msg, args);
    }

    public void debug(Throwable thrown) {
        debug("", thrown);
    }

    public void debug(String msg, Throwable thrown) {
        _logger.debug(msg, thrown);
    }

    public boolean isDebugEnabled() {
        return _logger.isDebugEnabled();
    }

    public void setDebugEnabled(boolean enabled) {
        warn("setDebugEnabled not implemented", null, null);
    }

    public Logger getLogger(String name) {
        return new JettySlf4jLogger(name);
    }

    @Override
    public String toString() {
        return _logger.toString();
    }
}
